package com.stylefeng.guns.modular.product.service.impl;

import com.stylefeng.guns.core.util.FmtMicrometer;
import com.stylefeng.guns.modular.product.service.ICustomerReportService;
import com.stylefeng.guns.modular.product.service.IProductFxService;
import com.stylefeng.guns.modular.product.warpper.FxReportWarpper;
import com.stylefeng.guns.modular.product.warpper.FxTotalReportWarpper;
import com.stylefeng.guns.modular.product.warpper.OptionReportWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustomerReportServiceImpl implements ICustomerReportService {

    @Autowired
    private IProductFxService productFxService;

    @Override
    public String getHtmlReportContent(String customer) {
        StringBuilder stringBuilder = new StringBuilder("<html><head> <style>\n" +
                "        table,table tr th, table tr td { border:1px solid #0094ff; }\n" +
                "        table {min-height: 25px; line-height: 25px; text-align: center; border-collapse: collapse; padding:2px;}   \n" +
                "    </style></head><body>");

        List<Map<String, Object>> fxList = productFxService.getFxReportByCustomer(customer);
        List<Map<String, Object>> fxData = (List<Map<String, Object>>)new FxReportWarpper(fxList).warp();
        getFxTable(customer, stringBuilder, fxData);

        List<Map<String, Object>> fxTotalList = productFxService.getFxTotalReportByCustomer(customer);
        List<Map<String, Object>> fxTotalData = (List<Map<String, Object>>)new FxTotalReportWarpper(fxTotalList).warp();
        getFxTotalTable(customer, stringBuilder, fxTotalData);

        List<Map<String, Object>> optionList = productFxService.getOptionReportByCustomer(customer);
        OptionReportWarpper optionReportWarpper = new OptionReportWarpper(optionList);
        //optionReportWarpper.warp();
        List<Map<String, Object>> optionData = (List<Map<String, Object>>)optionReportWarpper.warpTheList();
        getOptionTable(customer, stringBuilder, optionData);

        stringBuilder.append("</body></html>");
        return stringBuilder.toString();
    }

    private void getOptionTable(String customer, StringBuilder stringBuilder, List<Map<String, Object>> optionData) {
        if(null != optionData && optionData.size() > 0){
            stringBuilder.append("<p><Strong>"+customer+" - FX Option</strong></p>");
            stringBuilder.append("<table><thead><tr>" +
                    "<th style='width: 150px;'>Status</th>" +
                    "<th style='width: 150px;'>Expiry Date</th>" +
                    "<th style='width: 150px;'>Delivery Date</th>" +
                    "<th style='width: 150px;'>Trade Date</th>" +
                    "<th style='width: 150px;'>Buy/Sell</th>" +
                    "<th style='width: 150px;'>Product Level 3</th>" +
                    "<th style='width: 150px;'>Ccy1</th>" +
                    "<th style='width: 150px;'>Notional</th>" +
                    "<th style='width: 150px;'>Ccy2</th>" +
                    "<th style='width: 150px;'>Counter value</th>" +
                    "<th style='width: 150px;'>Strike</th>" +
                    "<th style='width: 150px;'>Premium (USD)</th>" +
                    "<th style='width: 150px;'>MTM (USD)</th>" +
                    "<th style='width: 150px;'>Delta (Ccy1)</th>" +
                    "<th style='width: 150px;'>Vega (USD)</th>" +
                    "<th style='width: 150px;'>Gamma (Ccy1)</th>" +
                    "<th style='width: 150px;'>Theta (USD)</th>" +
                    "<th style='width: 150px;'>Strategy Code</th>" +
                    "<th style='width: 150px;'>Gamma/Vega add-on</th></tr></thead>");
            stringBuilder.append("<tbody>");

            for (Map<String, Object> map : optionData) {
                stringBuilder.append("<tr>");
                stringBuilder.append("<td></td>");
                stringBuilder.append(this.getTableTd(map.get("expiryDate")));
                stringBuilder.append(this.getTableTd(map.get("deliveryDate")));
                stringBuilder.append(this.getTableTd(map.get("tradeDate")));
                stringBuilder.append(this.getTableTd(map.get("buySell")));
                stringBuilder.append(this.getTableTd(map.get("productLevel3")));
                stringBuilder.append(this.getTableTd(map.get("currency1")));
                stringBuilder.append(this.getTableTd(map.get("notional")));
                stringBuilder.append(this.getTableTd(map.get("currency2")));
                stringBuilder.append(this.getTableTd(map.get("counterValue")));
                stringBuilder.append(this.getTableTd(map.get("strike")));
                stringBuilder.append(this.getTableTd(map.get("premiumAmount")));
                stringBuilder.append(this.getTableTd(map.get("midPrice")));
                stringBuilder.append(this.getTableTd(map.get("delta")));
                stringBuilder.append(this.getTableTd(map.get("vega")));
                stringBuilder.append(this.getTableTd(map.get("gamma")));
                stringBuilder.append(this.getTableTd(map.get("theta")));
                stringBuilder.append(this.getTableTd(map.get("strategyCode")));
                stringBuilder.append(this.getTableTd(map.get("gammaVega")));
                stringBuilder.append("</tr>");
            }
            stringBuilder.append("</tbody></table>");

        }
    }

    private void getFxTotalTable(String customer, StringBuilder stringBuilder, List<Map<String, Object>> fxTotalList) {
        if(null != fxTotalList && fxTotalList.size() > 0){
            stringBuilder.append("<p><Strong>"+customer+" - FX Ladder</strong></p>");
            stringBuilder.append("<table><thead><tr>" +
                    "<th style='width: 150px;'>Value Date</th>" +
                    "<th style='width: 150px;'>Ccy1</th>" +
                    "<th style='width: 150px;'>Notional</th>" +
                    "<th style='width: 150px;'>Ccy2</th>" +
                    "<th style='width: 150px;'>Counter value</th>" +
                    "<th style='width: 150px;'>Rate</th>" +
                    "<th style='width: 150px;'>MTM (USD)</th>" +
                    "<th style='width: 150px;'>Strategy Code</th></tr></thead>");
            stringBuilder.append("<tbody>");
            for (Map<String, Object> map : fxTotalList) {
                stringBuilder.append("<tr>");
                stringBuilder.append(this.getTableTd(map.get("valueDate")));
                stringBuilder.append(this.getTableTd(map.get("currency1")));
                stringBuilder.append(this.getTableTd(map.get("notional")));
                stringBuilder.append(this.getTableTd(map.get("currency2")));
                stringBuilder.append(this.getTableTd(map.get("counterValue")));
                stringBuilder.append(this.getTableTd(map.get("rate")));
                stringBuilder.append(this.getTableTd(map.get("mtmUsd")));
                stringBuilder.append(this.getTableTd(map.get("strategyCode")));

                stringBuilder.append("</tr>");
            }
            stringBuilder.append("</tbody></table>");
        }
    }

    private void getFxTable(String customer, StringBuilder stringBuilder, List<Map<String, Object>> fxList) {
        if(null != fxList && fxList.size() > 0){
            stringBuilder.append("<p><Strong>"+customer+" - FX Breakdown</strong></p>");
            stringBuilder.append("<table><thead><tr>" +
                    "<th style='width: 150px;'>Value Date</th>" +
                    "<th style='width: 150px;'>Trade Date</th>" +
                    "<th style='width: 150px;'>Product Level 3</th>" +
                    "<th style='width: 150px;'>Ccy1</th>" +
                    "<th style='width: 150px;'>Notional</th>" +
                    "<th style='width: 150px;'>Ccy2</th>" +
                    "<th style='width: 150px;'>Counter value</th>" +
                    "<th style='width: 150px;'>Rate</th>" +
                    "<th style='width: 150px;'>MTM (USD)</th>" +
                    "<th style='width: 150px;'>Market Rate</th>" +
                    "<th style='width: 150px;'>Swap Point</th>" +
                    "<th style='width: 150px;'>Classification</th>" +
                    "<th style='width: 150px;'>Strategy Code</th></tr></thead>");
            stringBuilder.append("<tbody>");
            for (Map<String, Object> map : fxList) {
                stringBuilder.append("<tr>");
                stringBuilder.append(this.getTableTd(map.get("valueDate")));
                stringBuilder.append(this.getTableTd(map.get("tradeDate")));
                stringBuilder.append(this.getTableTd(map.get("productLevel3")));
                stringBuilder.append(this.getTableTd(map.get("currency1")));
                stringBuilder.append(this.getTableTd(map.get("notional")));
                stringBuilder.append(this.getTableTd(map.get("currency2")));
                stringBuilder.append(this.getTableTd(map.get("counterValue")));
                stringBuilder.append(this.getTableTd(map.get("rate")));
                stringBuilder.append(this.getTableTd(map.get("mtmUsd")));
                stringBuilder.append(this.getTableTd(map.get("marketRate")));
                stringBuilder.append(this.getTableTd(map.get("swapPoint")));
                stringBuilder.append(this.getTableTd(map.get("classification")));
                stringBuilder.append(this.getTableTd(map.get("strategyCode")));


                stringBuilder.append("</tr>");
            }
            stringBuilder.append("</tbody></table>");
        }
    }


    private String getTableTd(Object obj){
        if(obj == null)
            return "<td></td>";
        else
            return "<td>"+obj+"</td>";
    }
}
